package cn.edu.neusoft.cqy917.zyfypt_cqy917.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.R;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.LoginBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.LoginListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.model.LoginModel;

public class LoginActivity extends AppCompatActivity {
    TextView textView;
    TextView textView2;
    EditText editText;
    EditText editText2;
    Button button;
    Button button2;
    CheckBox checkBox;
    String str = "";
    SharedPreferences sharedPreferences;
    String sessionID="";
    LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID =loginBean.getSessionid();
            if (sessionID!=null){
                Toast.makeText(LoginActivity.this,sessionID,Toast.LENGTH_SHORT)
                        .show();
                saveSP();
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
            else {
                Toast.makeText(LoginActivity.this,
                "登陆失败",Toast.LENGTH_SHORT)
                        .show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        textView = (TextView) findViewById(R.id.textView);
        textView2 = (TextView) findViewById(R.id.textView2);
        editText = (EditText) findViewById(R.id.editText);
        editText2 = (EditText) findViewById(R.id.editText2);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        checkBox = (CheckBox)findViewById(R.id.checkBox);
        sharedPreferences =getSharedPreferences(
                "login",MODE_PRIVATE);
        readSP();


        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editText.getText().toString();
                String passwd = editText2.getText().toString();
                LoginModel loginModel = new LoginModel();
                loginModel.getLoginResult(username,passwd,loginListener);
            }
        });



    }
    private  void  saveSP(){
        String username =editText.getText().toString();
        String passwd =editText2.getText().toString();
        Boolean remember =checkBox.isChecked();
        SharedPreferences.Editor editor=
                sharedPreferences.edit();
        editor.putString("username",username);
        editor.putString("passwd",passwd);
        editor.putBoolean("remember",remember);
        editor.putString("sessionID",sessionID);
        editor.commit();
    }
    private  void  readSP(){
        String username =sharedPreferences
                .getString("username",null);
        String passwd =sharedPreferences
                .getString("passwd",null);
        Boolean remember =sharedPreferences
                .getBoolean("remember",false);
        if (remember){
            editText.setText(username);
            editText2.setText(passwd);
            checkBox.setChecked(remember);

        }

    }

}
